package
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	/**
	 * 
	 * @author hong.guo
	 * @description 方块工具类，包括常量
	 */	
	public class TileUtils
	{
		public static const STAGE_WIDTH:int = 240;
		public static const STAGE_HEIGHT:int = 480;
		public static const GAME_X:int = 80;
		public static const GAME_Y:int = 0;
		
		public static const TILE_X_NUM:int = 12;
		public static const TILE_Y_NUM:int = 24;
		public static const TILE_WIDTH:int = 20;
		public static const TILE_HEIGHT:int = 20;
		
		public static const BG_COLOR:uint = 0x999999;
		public static const BORDER_SIZE:uint = 1;
		public static const BORDER_COLOR:uint = 0xffffff;
		public static const PREVIEW_COLOR:uint = 0xffffff;
		public static const GAME_COLOR:uint = 0xffffff;
		public static const GAME_BORDER_COLOR:uint = 0x000000;
		public static const TILE_COLOR:uint = 0xff0000;
		public static const TIME_COUNT:int = 1000;
		public static const NEW_TILE_X:int = 4;
		public static const NEW_TILE_Y:int = 0;
		
		public static const BACK_TILE:int = 0;
		public static const FIXED_TILE:int = 1;
		public static const CURRENT_TILE:int = 2;
		
		public static const BACKGROUND_X:int = 0;
		public static const BACKGROUND_Y:int = 0;
		
		public static const PREVIEW_X_NUM:int = 12;
		public static const PREVIEW_Y_NUM:int = 4;
		public static const PREVIEW_X:int = 0;
		public static const PREVIEW_Y:int = 0;
				
		/**
		 * 0000 
		 */		
		public static const TYPE_1:int = 0;
		public static const TYPE_ARRAY_1:Array = [[0,0],[1,0],[2,0],[3,0],[1,-2],[1,-1],[1,0],[1,1],[0,0],[1,0],[2,0],[3,0],[1,-2],[1,-1],[1,0],[1,1]];
		/**
		 *   0
		 * 000 
		 */		
		public static const TYPE_2:int = 1;
		public static const TYPE_ARRAY_2:Array = [[2,0],[0,1],[1,1],[2,1],[0,0],[1,0],[1,1],[1,2],[0,1],[1,1],[2,1],[0,2],[0,0],[0,1],[0,2],[1,2]];
		/**
		 * 0
		 * 000 
		 */		
		public static const TYPE_3:int = 2;
		public static const TYPE_ARRAY_3:Array = [[0,0],[0,1],[1,1],[2,1],[1,0],[1,1],[0,2],[1,2],[0,1],[1,1],[2,1],[2,2],[0,0],[1,0],[0,1],[0,2]];
		/**
		 * 00
		 * 00 
		 */		
		public static const TYPE_4:int = 3;
		public static const TYPE_ARRAY_4:Array = [[1,0],[2,0],[1,1],[2,1],[1,0],[2,0],[1,1],[2,1],[1,0],[2,0],[1,1],[2,1],[1,0],[2,0],[1,1],[2,1]];
		/**
		 *  0
		 * 00
		 * 0 
		 */		
		public static const TYPE_5:int = 4;
		public static const TYPE_ARRAY_5:Array = [[2,0],[1,1],[2,1],[1,2],[0,1],[1,1],[1,2],[2,2],[2,0],[1,1],[2,1],[1,2],[0,1],[1,1],[1,2],[2,2]];
		/**
		 * 0
		 * 00
		 *  0 
		 */		
		public static const TYPE_6:int = 5;
		public static const TYPE_ARRAY_6:Array = [[1,0],[1,1],[2,1],[2,2],[1,0],[2,0],[0,1],[1,1],[1,0],[1,1],[2,1],[2,2],[1,0],[2,0],[0,1],[1,1]];
		/**
		 *  0
		 * 000 
		 */		
		public static const TYPE_7:int = 6;
		public static const TYPE_ARRAY_7:Array = [[1,0],[0,1],[1,1],[2,1],[1,0],[0,1],[1,1],[1,2],[0,1],[1,1],[2,1],[1,2],[1,0],[1,1],[2,1],[1,2]];
		
		public function TileUtils()
		{
		}
		
//		public static function drawTile(x:int, y:int, backColor:uint, borderColor:uint):Shape
//		{
//			var _titleShape:Shape = new Shape();
//			_titleShape.graphics.beginFill(backColor);
//			_titleShape.graphics.lineStyle(BORDER_SIZE,borderColor);
//			_titleShape.graphics.drawRect(x, y, TILE_WIDTH, TILE_HEIGHT);
//			_titleShape.graphics.endFill();
//			return _titleShape;
//		}
		
		public static function drawShape($tempShape:Shape, $x:int, $y:int, $backColor:uint, $borderColor:uint, $width:int=TILE_WIDTH, $height:int=TILE_HEIGHT, $alpha:Number=1):Shape
		{
			$tempShape.graphics.beginFill($backColor, $alpha);
			$tempShape.graphics.lineStyle(BORDER_SIZE,$borderColor);
			$tempShape.graphics.drawRect($x, $y, $width, $height);
			$tempShape.graphics.endFill();
			return $tempShape;
		}
		
		public static function createMask($width:int, $height:int, $x:int=0, $y:int=0, $color:uint=0x000000, $alpha:Number=0.6):Shape
		{
			var mask:Shape = new Shape();
			mask.graphics.beginFill($color, $alpha);
			mask.graphics.drawRect($x, $y, $width, $height);
			mask.graphics.endFill();
			return mask;
		}
		
		public static function createDefaultTextFormat($size:int=18,$color:uint=0x000000):TextFormat
		{
			var defaultFont:String = "宋体";
			var defaultSize:int = $size;
			var defaultColor:uint = $color;
			var format:TextFormat = new TextFormat(defaultFont,defaultSize,defaultColor);
			return format;
		}
		
		public static function checkHitBorder($targetX:int, $targetY:int, $valueX:int, $valueY:int):Boolean
		{
			if($targetX+$valueX >= TILE_X_NUM
				|| $targetX+$valueX < 0
				|| $targetY+$valueY >= TILE_Y_NUM)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public static function checkHitTile($targetX:int, $targetY:int, $valueX:int, $valueY:int):Boolean
		{
			var i:int = $targetX+$valueX;
			var j:int = $targetY+$valueY;
			if(i < 0 || i > TILE_X_NUM || j > TILE_Y_NUM || TileManager.instance.model.tileArr[i][j] == FIXED_TILE)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		
		public static function computeTileData($type:int, $direction:int):Array
		{
			switch ($type)
			{
				case TYPE_1:
					return TYPE_ARRAY_1.slice($direction*4, $direction*4+4);
					break;
				case TYPE_2:
					return TYPE_ARRAY_2.slice($direction*4, $direction*4+4);
					break;
				case TYPE_3:
					return TYPE_ARRAY_3.slice($direction*4, $direction*4+4);
					break;
				case TYPE_4:
					return TYPE_ARRAY_4.slice($direction*4, $direction*4+4);
					break;
				case TYPE_5:
					return TYPE_ARRAY_5.slice($direction*4, $direction*4+4);
					break;
				case TYPE_6:
					return TYPE_ARRAY_6.slice($direction*4, $direction*4+4);
					break;
				case TYPE_7:
					return TYPE_ARRAY_7.slice($direction*4, $direction*4+4);
					break;
				default:
					return null
					break;
			}
		}
				
		public static function removeParent($value:DisplayObject):void
		{
			if($value && $value.parent)
			{
				$value.parent.removeChild($value);
			}
		}
		
		public static function disposeAllChildren($target:DisplayObjectContainer):void
		{
			if($target == null)return;
			while($target.numChildren > 0)
			{
				var child:DisplayObject=$target.getChildAt(0);
				child = null;
			}
		}
	}
}